#!/usr/bin/env ruby
# frozen_string_literal: true

def gsub_file_content(path, old_content, new_content)
  path = File.expand_path(path, __dir__)

  # Support both old structure (files at root) and new structure (files in react_on_rails/)
  # This allows the script to work before and after the monorepo reorganization
  unless File.exist?(path)
    # Try the old location by removing one level of nesting
    old_path = path.sub(%r{/react_on_rails/Gemfile}, "/Gemfile")
    path = old_path if File.exist?(old_path)
  end

  content = File.binread(path)
  content.gsub!(old_content, new_content)
  File.binwrite(path, content)
end

def move(old_path, new_path)
  old_path = File.expand_path(old_path, __dir__)
  new_path = File.expand_path(new_path, __dir__)
  File.rename(old_path, new_path)
end

# Keep shakapacker.yml since we're using Shakapacker 8+
# move("../react_on_rails/spec/dummy/config/shakapacker.yml", "../react_on_rails/spec/dummy/config/webpacker.yml")

# Shakapacker - use version with async script loading support (8.2.0+)
gsub_file_content("../react_on_rails/Gemfile.development_dependencies", /gem "shakapacker", "[^"]*"/, 'gem "shakapacker", "8.2.0"')
gsub_file_content("../react_on_rails/spec/dummy/package.json", /"shakapacker": "[^"]*",/, '"shakapacker": "8.2.0",')

# The below packages don't work on the oldest supported Node version and aren't needed there anyway
# Note: All dev dependencies remain in root package.json even after workspace migration
gsub_file_content("../package.json", /"[^"]*eslint[^"]*": "[^"]*",?/, "")
gsub_file_content("../package.json", /"globals": "[^"]*",/, "")
gsub_file_content("../package.json", /"knip": "[^"]*",/, "")
gsub_file_content("../package.json", /"publint": "[^"]*",/, "")
gsub_file_content("../package.json", %r{"@arethetypeswrong/cli": "[^"]*",}, "")
gsub_file_content("../package.json", %r{"@testing-library/[^"]*": "[^"]*",}, "")

# Clean up any trailing commas before closing braces
gsub_file_content("../package.json", /,(\s*})/, "\\1")

# Switch to minimum supported React version (React 18 since we removed PropTypes)
gsub_file_content("../package.json", /"react": "[^"]*",/, '"react": "18.0.0",')
gsub_file_content("../package.json", /"react-dom": "[^"]*",/, '"react-dom": "18.0.0",')
gsub_file_content("../packages/react-on-rails-pro/package.json", /"react": "[^"]*",/, '"react": "18.0.0",')
gsub_file_content("../packages/react-on-rails-pro/package.json", /"react-dom": "[^"]*",/, '"react-dom": "18.0.0",')
gsub_file_content("../react_on_rails/spec/dummy/package.json", /"react": "[^"]*",/, '"react": "18.0.0",')
gsub_file_content("../react_on_rails/spec/dummy/package.json", /"react-dom": "[^"]*",/, '"react-dom": "18.0.0",')
gsub_file_content(
  "../packages/react-on-rails-pro/package.json",
  /"test:non-rsc": "(?:\\"|[^"])*",/,
  '"test:non-rsc": "jest tests --testPathIgnorePatterns=\".*(RSC|stream|' \
  'registerServerComponent|serverRenderReactComponent|SuspenseHydration).*\"",'
)
# test:rsc script now automatically detects React version and skips on React 18
# No override needed - the script checks React version and exits cleanly if < 19
# Keep modern JSX transform for React 18+
# gsub_file_content("../tsconfig.json", "react-jsx", "react")
# gsub_file_content("../spec/dummy/babel.config.js", "runtime: 'automatic'", "runtime: 'classic'")
# Keep modern ReScript configuration for React 18+
# gsub_file_content("../spec/dummy/rescript.json", '"version": 4', '"version": 4, "mode": "classic"')
# Skip React 16 file replacements since we're using React 18+
# Dir.glob(File.expand_path("../spec/dummy/**/app-react16/**/*.*", __dir__)).each do |file|
#   move(file, file.gsub("-react16", ""))
# end

# These replacements were incorrect - generateWebpackConfig() is the correct function from shakapacker
# gsub_file_content("../spec/dummy/config/webpack/commonWebpackConfig.js", /generateWebpackConfig(\(\))?/,
#                   "webpackConfig")
#
# gsub_file_content("../spec/dummy/config/webpack/webpack.config.js", /generateWebpackConfig(\(\))?/, "webpackConfig")
